/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.l2jfrozen.gameserver.network.clientpackets;

import java.io.PrintStream;
import java.nio.BufferUnderflowException;
import java.util.Collection;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.Random;
import java.util.Date;

import com.l2jfrozen.Config;
import com.l2jfrozen.gameserver.datatables.csv.MapRegionTable;
import com.l2jfrozen.gameserver.handler.IVoicedCommandHandler;
import com.l2jfrozen.gameserver.handler.VoicedCommandHandler;
import com.l2jfrozen.gameserver.managers.PetitionManager;
import com.l2jfrozen.gameserver.model.BlockList;
import com.l2jfrozen.gameserver.model.L2Character;
import com.l2jfrozen.gameserver.model.L2Object;
import com.l2jfrozen.gameserver.model.L2World;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance.PunishLevel;
import com.l2jfrozen.gameserver.network.SystemChatChannelId;
import com.l2jfrozen.gameserver.network.SystemMessageId;
import com.l2jfrozen.gameserver.network.serverpackets.CreatureSay;
import com.l2jfrozen.gameserver.network.serverpackets.SocialAction;
import com.l2jfrozen.gameserver.network.serverpackets.SystemMessage;
import com.l2jfrozen.gameserver.powerpak.PowerPak;
import com.l2jfrozen.gameserver.powerpak.PowerPakConfig;
import com.l2jfrozen.gameserver.util.Util;

import com.l2jfrozen.util.random.Rnd;
import com.l2jfrozen.gameserver.model.fmom.DummyChat;

public final class Say2 extends L2GameClientPacket
{
	private static Logger _log = Logger.getLogger(Say2.class.getName());
	private static Logger _logChat = Logger.getLogger("chat");

	public final static int ALL = 0;
	public final static int SHOUT = 1; //!
	public final static int TELL = 2;
	public final static int PARTY = 3; //#
	public final static int CLAN = 4; //@
	public final static int GM = 5; ////gmchat
	public final static int PETITION_PLAYER = 6; // used for petition
	public final static int PETITION_GM = 7; //* used for petition
	public final static int TRADE = 8; //+
	public final static int ALLIANCE = 9; //$
	public final static int ANNOUNCEMENT = 10; ////announce
	public final static int PARTYROOM_ALL = 16; //(Red)
	public final static int PARTYROOM_COMMANDER = 15; //(Yellow)
	public final static int HERO_VOICE = 17; //%

	private final static String[] CHAT_NAMES =
	{
			"ALL  ", "SHOUT", "TELL ", "PARTY", "CLAN ", "GM   ", "PETITION_PLAYER", "PETITION_GM", "TRADE", "ALLIANCE", "ANNOUNCEMENT", //10
			"WILLCRASHCLIENT:)",
			"FAKEALL?",
			"FAKEALL?",
			"FAKEALL?",
			"PARTYROOM_ALL",
			"PARTYROOM_COMMANDER",
			"HERO_VOICE"
	};

	private String _text;
	private int _type;
	private SystemChatChannelId _type2Check;
	private String _target;
	
	// game command: -roll
	private int Roll()
	{
		Random generator = new Random(new Date().getTime());
		int number = generator.nextInt(100);
		return number;
	}

	@Override
	protected void readImpl()
	{
		_text = readS();
		try
		{
			_type = readD();
			_type2Check = SystemChatChannelId.getChatType(_type);
			
		}
		catch(BufferUnderflowException e)
		{
			if(Config.ENABLE_ALL_EXCEPTIONS)
				e.printStackTrace();
			
			_type = CHAT_NAMES.length;
			_type2Check = SystemChatChannelId.CHAT_NONE;
		}
		_target = _type == TELL ? readS() : null;
	}

	@Override
	protected void runImpl()
	{
		if(Config.DEBUG)
		{
			_log.info("Say2: Msg Type = '" + _type + "' Text = '" + _text + "'.");
		}

		if(_type < 0 || _type >= CHAT_NAMES.length)
		{
			_log.warning("Say2: Invalid type: " + _type);
			return;
		}
		
		L2PcInstance activeChar = getClient().getActiveChar();
		
		// Anti-PHX Announce
		if (_type2Check == SystemChatChannelId.CHAT_NONE
				|| _type2Check == SystemChatChannelId.CHAT_ANNOUNCE
				|| _type2Check == SystemChatChannelId.CHAT_CRITICAL_ANNOUNCE
				|| _type2Check == SystemChatChannelId.CHAT_SYSTEM
				|| _type2Check == SystemChatChannelId.CHAT_CUSTOM
				|| (_type2Check == SystemChatChannelId.CHAT_GM_PET && !activeChar.isGM()))
		{
		   _log.warning("[Anti-PHX Announce] Illegal Chat channel was used by character: [" + activeChar.getName() + "]");
		   return;
		}
		
		if(activeChar == null)
		{
			_log.warning("[Say2.java] Active Character is null.");
			return;
		}
		
		if( activeChar.isChatBanned() && !activeChar.isGM() && _type != CLAN && _type != ALLIANCE && _type != PARTY)
		{
			activeChar.sendMessage("You may not chat while a chat ban is in effect.");
			return;
		}

		if(activeChar.isInJail() && Config.JAIL_DISABLE_CHAT)
		{
			if(_type == TELL || _type == SHOUT || _type == TRADE || _type == HERO_VOICE)
			{
				activeChar.sendMessage("You can not chat with players outside of the jail.");
				return;
			}
		}
		
		if (!getClient().getFloodProtectors().getSayAction().tryPerformAction("Say2"))
		{
			activeChar.sendMessage("You cannot speak too fast.");
			return;
		}
		
		if(activeChar.isCursedWeaponEquiped() && (_type == TRADE || _type == SHOUT))
		{
			activeChar.sendMessage("Shout and trade chatting cannot be used while possessing a cursed weapon.");
			return;
		}

		if(_type == PETITION_PLAYER && activeChar.isGM())
		{
			_type = PETITION_GM;
		}

		if(_text.length() > Config.MAX_CHAT_LENGTH)
		{
			if(Config.DEBUG)
			{
				_log.info("Say2: Msg Type = '" + _type + "' Text length more than " + Config.MAX_CHAT_LENGTH + " truncate them.");
			}
			_text = _text.substring(0, Config.MAX_CHAT_LENGTH);
			//return;
		}

		if(Config.LOG_CHAT)
		{
			LogRecord record = new LogRecord(Level.INFO, _text);
			record.setLoggerName("chat");

			if(_type == TELL)
			{
				record.setParameters(new Object[]
				{
						CHAT_NAMES[_type], "[" + activeChar.getName() + " to " + _target + "]"
				});
			}
			else
			{
				record.setParameters(new Object[]
				{
						CHAT_NAMES[_type], "[" + activeChar.getName() + "]"
				});
			}

			_logChat.log(record);
		}

		if (Config.L2WALKER_PROTEC && _type == TELL && checkBot(_text))
		{
			Util.handleIllegalPlayerAction(activeChar, "Client Emulator Detect: Player " + activeChar.getName() + " using l2walker.", Config.DEFAULT_PUNISH);
			return;
		}
		_text = _text.replaceAll("\\\\n", "");

		// Say Filter implementation
		if(Config.USE_SAY_FILTER)
		{
			checkText(activeChar);
		}
		
		if(PowerPakConfig.ENABLE_SAY_SOCIAL_ACTIONS && !activeChar.isAlikeDead() && !activeChar.isDead()){
			if ((_text.equalsIgnoreCase("hello") 
					|| _text.equalsIgnoreCase("hey") 
					|| _text.equalsIgnoreCase("aloha") 
					|| _text.equalsIgnoreCase("alo") 
					|| _text.equalsIgnoreCase("ciao")
					|| _text.equalsIgnoreCase("hi"))
					&& (!activeChar.isRunning() 
							|| !activeChar.isAttackingNow() 
							|| !activeChar.isCastingNow()
							|| !activeChar.isCastingPotionNow()))
				activeChar.broadcastPacket(new SocialAction(activeChar.getObjectId(), 2));
			
			if ((_text.equalsIgnoreCase("lol") 
					|| _text.equalsIgnoreCase("haha") 
					|| _text.equalsIgnoreCase("xaxa") 
					|| _text.equalsIgnoreCase("ghgh")
					|| _text.equalsIgnoreCase("jaja"))
					&& (!activeChar.isRunning() || !activeChar.isAttackingNow() || !activeChar.isCastingNow() || !activeChar.isCastingPotionNow()))
				activeChar.broadcastPacket(new SocialAction(activeChar.getObjectId(), 10));
				
			if ((_text.equalsIgnoreCase("yes") 
					|| _text.equalsIgnoreCase("si")
					|| _text.equalsIgnoreCase("yep"))
					&& (!activeChar.isRunning() || !activeChar.isAttackingNow() || !activeChar.isCastingNow()|| !activeChar.isCastingPotionNow()))
				activeChar.broadcastPacket(new SocialAction(activeChar.getObjectId(), 6));
			
			if ((_text.equalsIgnoreCase("no") 
					|| _text.equalsIgnoreCase("nop") 
					|| _text.equalsIgnoreCase("nope"))
					&& (!activeChar.isRunning() || !activeChar.isAttackingNow() || !activeChar.isCastingNow()|| !activeChar.isCastingPotionNow()))
				activeChar.broadcastPacket(new SocialAction(activeChar.getObjectId(), 5));
		
		}
		
		// by Azagthtot
		PowerPak.getInstance().chatHandler(activeChar, _type, _text);
		//CreatureSay cs = new CreatureSay(activeChar.getObjectId(),_type, activeChar.getName(), _text);

		L2Object saymode = activeChar.getSayMode();
		if(saymode != null)
		{
			String name = saymode.getName();
			int actor = saymode.getObjectId();
			_type = 0;
			Collection<L2Object> list = saymode.getKnownList().getKnownObjects().values();

			CreatureSay cs = new CreatureSay(actor, _type, name, _text);
			for(L2Object obj : list)
			{
				if(obj == null || !(obj instanceof L2Character))
				{
					continue;
				}
				L2Character chara = (L2Character) obj;
				chara.sendPacket(cs);
			}
			return;
		}
		
		CreatureSay cs = new CreatureSay(activeChar.getObjectId(), _type, activeChar.getName(), _text);
		switch(_type)
		{
			//*******************************
			case TELL:
			//*******************************

				// DummyChat Logging
				if(Config.DUMMY_CHAT_LOGS)
				{
					for (int i = 0; i < DummyChat.DCNAMES.length; i++)
					{
						if(DummyChat.DCNAMES[i].equalsIgnoreCase(_target))
						{
							activeChar.sendPacket(new CreatureSay(activeChar.getObjectId(), _type, "->" + DummyChat.DCNAMES[i], _text));
							try
							{
								PrintStream pst = new PrintStream(System.out, true, "CP866");
								System.setOut(pst);
								System.out.println("DCSay2: " + activeChar.getName() + " -> " + DummyChat.DCNAMES[i] + "  " + _text);
							}
							catch (Exception e)
							{
								e.printStackTrace(System.out);
							}
							return;
						}
					}
				}
				
				L2PcInstance receiver = L2World.getInstance().getPlayer(_target);
				
				if ((receiver != null) && (!BlockList.isBlocked(receiver, activeChar) || activeChar.isGM()))
				{
					if(receiver.isAway())
					{
						activeChar.sendMessage("Player is Away try again later.");
					}

					if(Config.JAIL_DISABLE_CHAT && receiver.isInJail())
					{
						activeChar.sendMessage("Player is in jail.");
						return;
					}

					if(receiver.isChatBanned() && !activeChar.isGM())
					{
						activeChar.sendMessage("Player is chat banned.");
						return;
					}
					
					/*if (receiver.isOffline())
					{
						activeChar.sendMessage("Player is in offline mode.");
						return;
					}*/
					
					if(!receiver.getMessageRefusal())
					{
						receiver.sendPacket(cs);
						activeChar.sendPacket(new CreatureSay(activeChar.getObjectId(), _type, "->" + receiver.getName(), _text));
						
						/** PMHook by WhatEver*/
						
						// @TODO
						// Add to config (enable/disable).
						
						if(Config.PMHOOK)
						{
							try
							{
								PrintStream pst = new PrintStream(System.out, true, "CP866");
								System.setOut(pst);
								System.out.println("TELL: " + activeChar.getName() + " -> " + receiver.getName() + "  " + _text);
							}
							catch (Exception e)
							{
								e.printStackTrace(System.out);
							}
						}
					}
					else
					{
						activeChar.sendPacket(new SystemMessage(SystemMessageId.THE_PERSON_IS_IN_MESSAGE_REFUSAL_MODE));
					}
				}
				else if(receiver != null && BlockList.isBlocked(receiver, activeChar))
				{
					SystemMessage sm = new SystemMessage(SystemMessageId.S1_HAS_ADDED_YOU_TO_IGNORE_LIST);
					sm.addString(_target);
					activeChar.sendPacket(sm);
					sm = null;
				}
				else
				{
					SystemMessage sm = new SystemMessage(SystemMessageId.S1_IS_NOT_ONLINE);
					sm.addString(_target);
					activeChar.sendPacket(sm);
					sm = null;
				}
				break;

			//*******************************
			case SHOUT:
			//*******************************

				// Flood protect Say
				if (!getClient().getFloodProtectors().getGlobalChat().tryPerformAction("global chat"))
					return;

		           if(Config.DEFAULT_GLOBAL_CHAT.equalsIgnoreCase("on") || Config.DEFAULT_GLOBAL_CHAT.equalsIgnoreCase("gm") && activeChar.isGM())
		           {
		              int region = MapRegionTable.getInstance().getMapRegion(activeChar.getX(), activeChar.getY());
		              for(L2PcInstance player : L2World.getInstance().getAllPlayers())
		              {
		                 if(region == MapRegionTable.getInstance().getMapRegion(player.getX(), player.getY()))
		                 {
		                    player.sendPacket(cs);
		                 }
		              }
		           }
		           else if(Config.DEFAULT_GLOBAL_CHAT.equalsIgnoreCase("global"))
		           {
		              if(Config.GLOBAL_CHAT_WITH_PVP)
		              {
		                 if((activeChar.getPvpKills() < Config.GLOBAL_PVP_AMOUNT) && !activeChar.isGM())
		                 {
		                    activeChar.sendMessage("You must have at least " + Config.GLOBAL_PVP_AMOUNT+ " pvp kills in order to speak in global chat");
		                    return;
		                 }
		                 for(L2PcInstance player : L2World.getInstance().getAllPlayers())
		                 {
		                    player.sendPacket(cs);
		                 }
		              }
		              else
		              {
		                 for(L2PcInstance player : L2World.getInstance().getAllPlayers())
		                 {
		                    player.sendPacket(cs);
		                 }
		              }
		           }
		           break;

				//*******************************
				case TRADE:
				//*******************************

		           if(Config.DEFAULT_TRADE_CHAT.equalsIgnoreCase("on"))
		           {
		              if(Config.TRADE_CHAT_WITH_PVP)
		              {
		                 if((activeChar.getPvpKills() <= Config.TRADE_PVP_AMOUNT) && !activeChar.isGM())
		                 {
		                    activeChar.sendMessage("You must have at least " + Config.TRADE_PVP_AMOUNT+ "  pvp kills in order to speak in trade chat");
		                    return;
		                 }
		                 for(L2PcInstance player : L2World.getInstance().getAllPlayers())
		                 {
		                    player.sendPacket(cs);
		                 }
		              }
		              else
		              {
		                 for(L2PcInstance player : L2World.getInstance().getAllPlayers())
		                 {
		                    player.sendPacket(cs);
		                 }
		              }
		           }
		           else if(Config.DEFAULT_TRADE_CHAT.equalsIgnoreCase("limited"))
		           {
		        	   
					if(Config.TRADE_CHAT_IS_NOOBLE)
					{
						if(!activeChar.isNoble() && !activeChar.isGM())
						{
							activeChar.sendMessage("Only Nobless Players Can Use This Chat");
							return;
						}
						
						int region = MapRegionTable.getInstance().getMapRegion(activeChar.getX(), activeChar.getY());
						for(L2PcInstance player : L2World.getInstance().getAllPlayers())
						{
							if(region == MapRegionTable.getInstance().getMapRegion(player.getX(), player.getY()))
							{
								player.sendPacket(cs);
							}
						}
						
					}
					else
					{
						int region = MapRegionTable.getInstance().getMapRegion(activeChar.getX(), activeChar.getY());
						for(L2PcInstance player : L2World.getInstance().getAllPlayers())
						{
							if(region == MapRegionTable.getInstance().getMapRegion(player.getX(), player.getY()))
							{
								player.sendPacket(cs);
							}
						}
					}

					
		           }
		           break;

			//*******************************
			case ALL:
			//*******************************

				/**
				 * Some Dota Commands
				 * by WhatEver
				 */

				String wtfText = "-wtf";
				String apText = "-ap";
				String arText = "-ar";
				String rdText = "-rd";
				String emText = "-em";
				String apemText = "-apem";
				String aremText = "-arem";
				String rdemText = "-rdem";
				
				String randomText = "-random";
				String rollText = "-roll";
				String msTextShort = "-ms";
				String msTextLong = "-movespeed";
				String csonText = "-cson";
				String csoffText = "-csoff";
				String donText = "-don";
				String hhnText = "-hhn";
				String invokelistText = "-invokelist";
				String unlockText = "-unlock";
				String gameinfoText = "-gameinfo";
				
				
				///////////////
				// -wtf
				
				if(wtfText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay wtf1 = new CreatureSay(0, 16, "SYSTEM", "WTF Mode activated. In this mode spells have no cooldown or manacost.");
						CreatureSay wtf2 = new CreatureSay(0, 16, "SYSTEM", "This is a FOR FUN ONLY mode.");
						pc.sendPacket(wtf1);
						pc.sendPacket(wtf2);
					}
				}
				
				///////////////
				// -ap
				
				if(apText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay ap = new CreatureSay(0, 16, "SYSTEM", activeChar.getName() + " has selected All Pick");
						pc.sendPacket(ap);
					}
					activeChar.sendMessage("For more information about the game modes use -gameinfo.");
				}
				
				///////////////
				// -ar
				
				if(arText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay ar = new CreatureSay(0, 16, "SYSTEM", activeChar.getName() + " has selected All Random");
						pc.sendPacket(ar);
					}
					activeChar.sendMessage("For more information about the game modes use -gameinfo.");
				}
				
				///////////////
				// -rd
				
				if(rdText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay rd = new CreatureSay(0, 16, "SYSTEM", activeChar.getName() + " has selected Random Draft");
						pc.sendPacket(rd);
					}
					activeChar.sendMessage("For more information about the game modes use -gameinfo.");
				}
				
				///////////////
				// -em
				
				if(emText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay em = new CreatureSay(0, 16, "SYSTEM", activeChar.getName() + " has selected Easy Mode");
						pc.sendPacket(em);
					}
					activeChar.sendMessage("For more information about the game modes use -gameinfo.");
				}
				
				///////////////
				// -apem
				
				if(apemText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay apem = new CreatureSay(0, 16, "SYSTEM", activeChar.getName() + " has selected All Pick / Easy Mode");
						pc.sendPacket(apem);
					}
					activeChar.sendMessage("For more information about the game modes use -gameinfo.");
				}
				
				///////////////
				// -arem
				
				if(aremText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay arem = new CreatureSay(0, 16, "SYSTEM", activeChar.getName() + " has selected All Random / Easy Mode");
						pc.sendPacket(arem);
					}
					activeChar.sendMessage("For more information about the game modes use -gameinfo.");
				}
				
				///////////////
				// -rdem
				
				if(rdemText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay rdem = new CreatureSay(0, 16, "SYSTEM", activeChar.getName() + " has selected Random Draft / Easy Mode");
						pc.sendPacket(rdem);
					}
					activeChar.sendMessage("For more information about the game modes use -gameinfo.");
				}
				
				///////////////
				// -random
				
				if(randomText.equalsIgnoreCase(_text))
				{
					activeChar.sendMessage("You will be given a random hero soon.");
				}
				
				///////////////
				// -roll
				
				if(rollText.equalsIgnoreCase(_text))
				{
					int mainNumb = Roll();
					
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay rl = new CreatureSay(0, 15, "SYSTEM", activeChar.getName() + " has rolled " + mainNumb + " out of 100.");
						pc.sendPacket(rl);
					}
				}
				
				///////////////
				// -ms
				
				if(msTextShort.equalsIgnoreCase(_text) || msTextLong.equalsIgnoreCase(_text))
				{
					float ms = activeChar.getStat().getMoveSpeed();
					activeChar.sendMessage("Your movement speed is " + ms + ".");
				}
				
				///////////////
				// -cson
				
				if(csonText.equalsIgnoreCase(_text))
				{
					activeChar.sendMessage("Creep stats on.");
				}
				
				///////////////
				// -csoff
				
				if(csoffText.equalsIgnoreCase(_text))
				{
					activeChar.sendMessage("Creep stats off.");
				}
				
				///////////////
				// -don
				
				if(donText.equalsIgnoreCase(_text))
				{
					activeChar.sendMessage("The death timer will show up when you die.");
				}
				
				///////////////
				// -hhn
				
				if(hhnText.equalsIgnoreCase(_text))
				{
					activeChar.sendMessage("Hero names in chat will now be hidden.");
				}
				
				///////////////
				// -invokelist
				
				if(invokelistText.equalsIgnoreCase(_text))
				{
					activeChar.sendMessage("Sun Strike [EEE] - [T]");
					activeChar.sendMessage("Chaos Meteor [EEW] - [D]");
					activeChar.sendMessage("Forge Spirit [EEQ] - [F]");
					activeChar.sendMessage("Cold Snap [QQQ] - [Y]");
					activeChar.sendMessage("Ghost Walk [QQW] - [V]");
					activeChar.sendMessage("Ice Wall [QQE] - [G]");
					activeChar.sendMessage("Alacrity [WWW] - [Z]");
					activeChar.sendMessage("Tornado [WWQ] - [X]");
					activeChar.sendMessage("EMP [WWE] - [C]");
					activeChar.sendMessage("Deafening Blast [QWE] - [B]");
				}
				
				///////////////
				// -unlock
				
				if(unlockText.equalsIgnoreCase(_text))
				{
					for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
					{
						CreatureSay un = new CreatureSay(0, 15, "SYSTEM", activeChar.getName() + " voted for unlock players.");
						pc.sendPacket(un);
					}
				}
				
				///////////////
				// -gameinfo
				
				if(gameinfoText.equalsIgnoreCase(_text))
				{
					activeChar.sendMessage("Admin is too lame for change this command!");
				}
				
				// TODO:
				String DevTeam = "-dev";
				String Author = "-author";
				String RockTheVote = "-rtv";
				
				///////////////
				// -dev || -author
				
				if(DevTeam.equalsIgnoreCase(_text) || Author.equalsIgnoreCase(_text))
				{
					activeChar.sendMessage("===== ADMINISTRATION =====");
					activeChar.sendMessage("Powered by WhatEver");
					activeChar.sendMessage("E-mail: dpstrangers@gmail.com");
				}
				
				///////////////
				// -rtv
				
				if(RockTheVote.equalsIgnoreCase(_text))
				{
					if(activeChar.getItemCount(9999, 0) >= 1)
					{
						activeChar.getInventory().destroyItemByItemId("", 9999, 2, activeChar, null);
						
						for(L2PcInstance pc : L2World.getInstance().getAllPlayers())
						{
							CreatureSay rtv = new CreatureSay(0, 15, "SYSTEM", "Player " + activeChar.getName() + " voted for server restarting.");
							pc.sendPacket(rtv);
						}
					}
					else
					{
						activeChar.sendMessage("You dont have enough Coin of Kharden.");
					}
				}
				
				////////////////////////////////////////////////////////
				
				if(_text.startsWith("."))
				{	
					StringTokenizer st = new StringTokenizer(_text);
					IVoicedCommandHandler vch;
					String command = "";
					String target = "";

					if(st.countTokens() > 1)
					{
						command = st.nextToken().substring(1);
						target = _text.substring(command.length() + 2);
						vch = VoicedCommandHandler.getInstance().getVoicedCommandHandler(command);
					}
					else
					{
						command = _text.substring(1);
						if(Config.DEBUG)
						{
							_log.info("Command: " + command);
						}
						vch = VoicedCommandHandler.getInstance().getVoicedCommandHandler(command);
					}

					if(vch != null)
					{
						vch.useVoicedCommand(command, activeChar, target);
						break;
					}
				}
				
				for(L2PcInstance player : activeChar.getKnownList().getKnownPlayers().values())
				{
					if(player != null && activeChar.isInsideRadius(player, 1250, false, true))
					{
						player.sendPacket(cs);
					}
				}
				activeChar.sendPacket(cs);
				
				break;
			case CLAN:
				if(activeChar.getClan() != null)
				{
					activeChar.getClan().broadcastToOnlineMembers(cs);
				}
				break;
			case ALLIANCE:
				if(activeChar.getClan() != null)
				{
					activeChar.getClan().broadcastToOnlineAllyMembers(cs);
				}
				break;
			case PARTY:
				if(activeChar.isInParty())
				{
					activeChar.getParty().broadcastToPartyMembers(cs);
				}
				break;
			case PETITION_PLAYER:
			case PETITION_GM:
				if(!PetitionManager.getInstance().isPlayerInConsultation(activeChar))
				{
					activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_NOT_IN_PETITION_CHAT));
					break;
				}

				PetitionManager.getInstance().sendActivePetitionMessage(activeChar, _text);
				break;
			case PARTYROOM_ALL:
				if(activeChar.isInParty())
				{
					if(activeChar.getParty().isInCommandChannel() && activeChar.getParty().isLeader(activeChar))
					{
						activeChar.getParty().getCommandChannel().broadcastCSToChannelMembers(cs, activeChar);
					}
				}
				break;
			case PARTYROOM_COMMANDER:
				if(activeChar.isInParty())
				{
					if(activeChar.getParty().isInCommandChannel() && activeChar.getParty().getCommandChannel().getChannelLeader().equals(activeChar))
					{
						activeChar.getParty().getCommandChannel().broadcastCSToChannelMembers(cs, activeChar);
					}
				}
				break;
			case HERO_VOICE:
				if(activeChar.isGM())
				{
					for(L2PcInstance player : L2World.getInstance().getAllPlayers())
						if(!BlockList.isBlocked(player, activeChar))
						{
							player.sendPacket(cs);
						}
				}
				else if(activeChar.isHero())
				{
					// Flood protect Hero Voice
					if (!getClient().getFloodProtectors().getHeroVoice().tryPerformAction("hero voice"))
						return;

					for(L2PcInstance player : L2World.getInstance().getAllPlayers())
						if(!BlockList.isBlocked(player, activeChar))
						{
							player.sendPacket(cs);
						}
				}
				break;
		}
	}
	
	private static final String[] WALKER_COMMAND_LIST = { "USESKILL", "USEITEM", "BUYITEM", "SELLITEM", "SAVEITEM", "LOADITEM", "MSG", "SET", "DELAY", "LABEL", "JMP", "CALL",
		"RETURN", "MOVETO", "NPCSEL", "NPCDLG", "DLGSEL", "CHARSTATUS", "POSOUTRANGE", "POSINRANGE", "GOHOME", "SAY", "EXIT", "PAUSE", "STRINDLG", "STRNOTINDLG", "CHANGEWAITTYPE",
		"FORCEATTACK", "ISMEMBER", "REQUESTJOINPARTY", "REQUESTOUTPARTY", "QUITPARTY", "MEMBERSTATUS", "CHARBUFFS", "ITEMCOUNT", "FOLLOWTELEPORT" };
	
	
	
	private boolean checkBot(String text)
	{
		for (String botCommand : WALKER_COMMAND_LIST)
		{
			if (text.startsWith(botCommand))
				return true;
		}
		return false;
	}
	

	private void checkText(L2PcInstance activeChar)
	{
		if(Config.USE_SAY_FILTER)
		{
			String filteredText = _text.toLowerCase();
			
			for(String pattern : Config.FILTER_LIST)
			{
				filteredText = filteredText.replaceAll("(?i)" + pattern, Config.CHAT_FILTER_CHARS);
			}
			
			if(!filteredText.equalsIgnoreCase(_text))
			{				
				if(Config.CHAT_FILTER_PUNISHMENT.equalsIgnoreCase("chat"))
				{
					activeChar.setPunishLevel(PunishLevel.CHAT, Config.CHAT_FILTER_PUNISHMENT_PARAM1);
					activeChar.sendMessage("Administrator banned you chat from " + Config.CHAT_FILTER_PUNISHMENT_PARAM1 + " minutes");
				}
				else if(Config.CHAT_FILTER_PUNISHMENT.equalsIgnoreCase("karma"))
				{
					activeChar.setKarma(Config.CHAT_FILTER_PUNISHMENT_PARAM2);
					activeChar.sendMessage("You have get " + Config.CHAT_FILTER_PUNISHMENT_PARAM2 + " karma for bad words");
				}
				else if(Config.CHAT_FILTER_PUNISHMENT.equalsIgnoreCase("jail"))
				{
					activeChar.setPunishLevel(PunishLevel.JAIL, Config.CHAT_FILTER_PUNISHMENT_PARAM1);
				}	
				activeChar.sendMessage("The word "+_text+" is not allowed!");
				_text = filteredText;
			}
		}
	}

	@Override
	public String getType()
	{
		return "[C] 38 Say2";
	}
}